/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import Utility.MemberAccountUtility;
import Utility.ThreadOperationsUtility;
import Utility.Utility;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionRedirect;
import xmlMapping.Forum;
import xmlMapping.Member;

/**
 *
 * @author Ruba
 */
public class AddEditThreadAction extends DispatchAction {

     public ActionForward edit(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String body = ((beans.ThreadActionForm)form).getThreadBody();
        String title = ((beans.ThreadActionForm)form).getThreadTitle();
        System.out.println("bode"+body);
        System.out.println("title"+title);
         String threadDescription=((beans.ThreadActionForm)form).getThreadDescription();
         
        xmlMapping.Thread th= new ThreadOperationsUtility().getThread(((beans.ThreadActionForm)form).getId());
        Member mem = new MemberAccountUtility().getAccountById((Integer)request.getSession().getAttribute("id"));
        th.setBody(body);
        th.setTitle(title);
        th.setThread_description(threadDescription);
        new ThreadOperationsUtility().editThread(th,mem);
        ActionRedirect redirect = new ActionRedirect(mapping.findForward("threadPage"));
        redirect.addParameter("threadid",Integer.parseInt(request.getParameter("threadid")) );
        return redirect;
    }

    public ActionForward add(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        String body = ((beans.ThreadActionForm)form).getThreadBody();
        String title = ((beans.ThreadActionForm)form).getThreadTitle();
                String threadDescription=((beans.ThreadActionForm)form).getThreadDescription();
                       
        System.out.println("##title"+title);
        System.out.println("## body "+body);
        Member mem = new MemberAccountUtility().getAccountById((Integer)request.getSession().getAttribute("id"));
        System.out.println("after get member");
        Forum forum = new Utility().getForumById(((beans.ThreadActionForm)form).getId());
        System.out.println("## get forum done");
        xmlMapping.Thread th= new xmlMapping.Thread(mem,forum,title,body,false,false,false,0,sqlDate);
        System.out.println("## thread creation done");
        th.setNoofpost(new Integer(0));
        th.setNoOfVisitor(new Integer(0));
         th.setThread_description(threadDescription);
        new ThreadOperationsUtility().addThread(th);
        System.out.println("## after add thread");
        ActionRedirect redirect = new ActionRedirect(mapping.findForward("forumPage"));
        redirect.addParameter("forumId",Integer.parseInt(request.getParameter("forumId")) );
        return redirect;
    }

     public ActionForward delete(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         Member mem = new MemberAccountUtility().getAccountById((Integer)request.getSession().getAttribute("id"));
         System.out.println("after get member @ delete dispatcher");
         new ThreadOperationsUtility().removeThread(Integer.parseInt(request.getParameter("threadId")),mem);
         System.out.println("after remove thread @ delete dispatcher");
         ActionRedirect redirect = new ActionRedirect(mapping.findForward("forumPage"));
         redirect.addParameter("forumId",Integer.parseInt(request.getParameter("forumId")) );
         return redirect;
    }
}